home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / Printing / GrayText / GrayText.p < prev    next >
Encoding:
Text File  |  1992-07-15  |  5.5 KB  |  219 lines  |  [TEXT/MPS ]

  1. {**
  2.  **     Program: GrayText
  3.  **
  4.  **     Version: 1.0    7/5/91
  5.  **
  6.  **        MPW 3.2 Pascal source.
  7.  **
  8.  **     Purpose:
  9.  **
  10.  **        GrayText demonstrates two methods of creating grayscale text.  On PostScript
  11.  **        printers it sends PostScript, on QuickDraw printers it sends Color QuickDraw calls. 
  12.  **        If a QuickDraw printer does not use a CGrafPort, the gray text comes out as black.
  13.  **        Unfortunately, there's currently no good way to handle that situation.
  14.  **        
  15.  **        Note: I'm assuming Color QuickDraw is present, but the app should really check for
  16.  **        that with Gestalt.
  17.  **
  18.  **        - Dave Hersey
  19.  **
  20.  **        Macintosh Developer Technical Support
  21.  **
  22.  **
  23.  **}
  24.  
  25. PROGRAM GrayText;
  26.  
  27. USES
  28.     MemTypes, QuickDraw, OSIntf, ToolIntf, Traps, MacPrint, Packages, PrintComments;
  29.  
  30.  
  31. {*------ SendPostScript ------------------------------------------------------------*}
  32.  
  33. PROCEDURE SendPostScript(theComment: Str255);
  34. VAR
  35.     PSCommand    : Str255;
  36.     CommandHdl    : Handle;
  37.     CRString    : Str255;
  38.     theError    : OSErr;
  39. BEGIN
  40.     CRString := ' ';
  41.     CRString[1] := CHR(13);
  42.     PSCommand := theComment;
  43.     PSCommand := CONCAT(PSCommand, CRString);
  44.     theError := PtrToHand(POINTER(ORD(@PSCommand) + 1), CommandHdl, LENGTH(PSCommand));
  45.     PicComment(PostScriptHandle, LENGTH(PSCommand), CommandHdl);
  46.     DisposHandle(CommandHdl);
  47. END;
  48.  
  49.  
  50. {*------ DrawStuff -----------------------------------------------------------------*}
  51.  
  52. {**
  53.  **      DrawStuff will send some PostScript that draws gray text to a printer
  54.  **        that talks PostScript. It will also send the correct QuickDraw
  55.  **        representation of this to printers that do not talk PostScript.
  56.  **}
  57.  
  58.  PROCEDURE DrawStuff (boundsRect : Rect; theGPort : GrafPtr);
  59.  
  60.  VAR
  61.    oldPort  :     GrafPtr;
  62.    PSHdl    :   Handle;
  63.    fNum        :    Integer;
  64.    rgb        :    RGBColor;
  65.    
  66.  BEGIN
  67.  
  68.     GetPort (oldPort);
  69.     SetPort (theGPort);
  70.  
  71.     PenSize(0, 0);
  72.     MoveTo(10, 10); 
  73.     Line(0, 0);
  74.     PenSize(1, 1);
  75.     
  76.     {**
  77.      ** This line tells the LaserWriter driver to ignore Quickdraw calls until it    
  78.      ** receives a PostScriptEnd picture comment.  This line is ignored by Quickdraw 
  79.      ** printer drivers (ie. the ImageWriter driver).
  80.      **}
  81.     
  82.     PicComment (PostScriptBegin, 0, NIL);
  83.  
  84.         {**
  85.          ** QuickDraw representation of the document. These calls
  86.          ** will only be executed by QuickDraw printers.
  87.          **}
  88.     
  89.     GetFNum('Times', fNum);        (* Set the font to Times Bold Italic 96 pt. *)
  90.     TextFont(fNum);
  91.     TextSize(96);
  92.     TextFace([bold, italic]);
  93.  
  94.     rgb.red := 52428;            (* 80% of 65535. *)
  95.     rgb.green := 52428;
  96.     rgb.blue := 52428;
  97.     RGBForeColor(rgb);            (* Set an 80% gray pen. *)
  98.     MoveTo(10, 100);            (* Move and draw. *)
  99.     DrawString('Sample Text.');
  100.     ForeColor(blackColor);        (* Reset foreground color. *)
  101.  
  102.         {**
  103.          ** PostScript representation of the document. These calls
  104.          ** will only be executed by PostScript printers.
  105.          **}
  106.  
  107.     SendPostScript('0 760 translate 1 -1 scale');
  108.     SendPostScript('/Times-BoldItalic findfont 96 scalefont setfont');
  109.     SendPostScript('10 660 moveto .8 setgray (Sample Text.) show');
  110.          
  111.     {**
  112.      ** This comment tells the LaserWriter driver tro start executing 
  113.      ** QuickDraw calls normally.
  114.      **}
  115.  
  116.     PicComment (PostScriptEnd, 0, NIL);
  117.     SetPort(oldPort);
  118.  
  119.  END;  {**  DrawStuff  **}
  120.  
  121.  
  122.  
  123.  
  124. {*------ PrintStuff ----------------------------------------------------------------*}
  125. {**
  126.  **        PrintStuff will call all of the necessary Print Manager calls to print 
  127.  **        a document. It checks PrError() after each Print Manager call. If an error 
  128.  **     is found, all of the Print Manager open calls (i.e. PrOpen, PrOpenDoc...) 
  129.  **        will have a corresponding close call before the error is posted to the user. 
  130.  **        You want to use this approach to make sure the Print Manager closes properly 
  131.  **        and all temporary memory is released.
  132.  **}
  133.  
  134. PROCEDURE PrintStuff;
  135.  
  136. VAR
  137.   Loop,
  138.   NumberOfPages,
  139.   PageNumber        : Integer;
  140.   PrintError        : LongInt;
  141.   oldPort              : GrafPtr;
  142.   thePrRecHdl        : THPrint;
  143.   thePrPort            : TPPrPort;
  144.   theStatus            : TPrStatus;
  145.     
  146. BEGIN
  147.    GetPort(oldPort);
  148.     
  149.    thePrRecHdl := THPrint(NewHandle(SIZEOF(TPrint)));
  150.     
  151.    IF (MemError = noErr) AND (thePrRecHdl <> NIL) THEN
  152.     BEGIN
  153.        PrOpen;
  154.        IF (PrError = noErr) THEN
  155.         BEGIN
  156.            PrintDefault(thePrRecHdl);
  157.  
  158.            IF (PrError = noErr) THEN
  159.             BEGIN
  160.                IF (PrStlDialog(thePrRecHdl)) THEN
  161.                 BEGIN
  162.                    IF (PrJobDialog(thePrRecHdl)) THEN 
  163.                     BEGIN
  164.                           thePrPort := PrOpenDoc(thePrRecHdl, NIL, NIL);
  165.  
  166.                       IF (PrError = noErr) THEN
  167.                         BEGIN
  168.  
  169.                              PrOpenPage(thePrPort, NIL);
  170.                                 
  171.                           IF (PrError = noErr) THEN
  172.                             BEGIN
  173.                               {**
  174.                                   rPage (IM II-150) is the printable area for the  
  175.                                   currently selected printer. By passing the current  
  176.                                   port to the draw routine, enables your app
  177.                                   to use the same routine to draw to the screen
  178.                                   and the printer's GrafPort.
  179.                                **}
  180.                                     
  181.                                DrawStuff (thePrRecHdl^^.prInfo.rPage, 
  182.                                            GrafPtr (thePrPort));
  183.                                  
  184.                              END;
  185.                             PrClosePage(thePrPort);
  186.                           END;
  187.                              
  188.                           PrCloseDoc(thePrPort);
  189.                              
  190.                           IF (thePrRecHdl^^.prJob.bJDocLoop = bSpoolLoop) and (PrError = noErr) THEN
  191.                                PrPicFile(thePrRecHdl, NIL, NIL, NIL, theStatus);
  192.  
  193.                       END;
  194.                   END;
  195.               END;
  196.           END;
  197.         
  198.         PrClose;
  199.  
  200.      END;
  201. END;  {**  PrintStuff  **}
  202.  
  203.  
  204. {*------ main ----------------------------------------------------------------------*}
  205.  
  206. BEGIN
  207.     
  208.     InitGraf(@thePort);
  209.     InitFonts;
  210.     FlushEvents(everyEvent, 0);    
  211.     InitWindows;
  212.     InitMenus;
  213.     TEInit;
  214.     InitDialogs(NIL);
  215.     InitCursor;
  216.  
  217.     PrintStuff;
  218.  
  219. END. {**  main  **}